package com.greenantmall.adapter;

import java.util.List;

import com.greenantmall.R;
import com.greenantmall.activity.MyAddressActivity;
import com.greenantmall.activity.MyAddressEditActivity;
import com.greenantmall.bean.ShippingAddr;
import com.greenantmall.service.RetrieveDataAsyn;
import com.greenantmall.service.RetrieveDataListener;
import com.greenantmall.utils.UtilTools;
import com.greenantmall.view.SingleChooseDialog;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.TextView;

public class MyAddressListAdapter extends BaseAdapter
{

    private List<ShippingAddr> list;
    private Context context;
    //BitmapUtils bitmapUtils;

    public MyAddressListAdapter(List<ShippingAddr> list, Context ctx)
    {
        this.list = list;
        this.context = ctx;
    }

    @Override
    public int getCount()
    {
        return list.size();
    }

    @Override
    public Object getItem(int position)
    {
        return list.get(position);
    }

    @Override
    public long getItemId(int position)
    {
        return position;
    }

    @Override
    public View getView(final int position, View convertView, ViewGroup parent)
    {

        ViewHolder viewHolder = null;
        if (convertView == null)
        {
            LayoutInflater inflater = ((Activity) context).getLayoutInflater();
            convertView = inflater.inflate(R.layout.myaddressitem, null);
            viewHolder = new ViewHolder();

            viewHolder.name = (TextView) convertView.findViewById(R.id.name);
            viewHolder.mobile = (TextView) convertView.findViewById(R.id.mobile);
            viewHolder.uptown = (TextView) convertView.findViewById(R.id.uptown);
            viewHolder.door = (TextView) convertView.findViewById(R.id.door);
            viewHolder.defaultView = (View) convertView.findViewById(R.id.defaultView);
            viewHolder.edit = (Button) convertView.findViewById(R.id.edit);
            convertView.setTag(viewHolder);

        } else
        {
            viewHolder = (ViewHolder) convertView.getTag();
        }
        
        if(position==0)
        {
        	viewHolder.defaultView.setVisibility(View.VISIBLE);
        }else{
        	viewHolder.defaultView.setVisibility(View.GONE);
        }
        final ShippingAddr goods = list.get(position);

        viewHolder.name.setText(goods.getName());
        viewHolder.mobile.setText(goods.getMobile());
        viewHolder.uptown.setText(goods.getUptown());
        final String door = goods.getDoor();
        viewHolder.door.setText(door);
        convertView.setOnClickListener(new View.OnClickListener()
        {

            @Override
            public void onClick(View v)
            {

                if (((MyAddressActivity) context).getMode() == MyAddressActivity.MODE_CHOOSE)
                {
                    Intent intent = new Intent();
                    Bundle bundle = new Bundle();
                    bundle.putString(MyAddressEditActivity.KEY_ADDRESS_OBJECTID, goods.getObjectId());
                    bundle.putString(MyAddressEditActivity.KEY_ADDRESS_NAME, goods.getName());
                    bundle.putString(MyAddressEditActivity.KEY_ADDRESS_MOBILE, goods.getMobile());
                    bundle.putString(MyAddressEditActivity.KEY_ADDRESS_UPTOWN, goods.getUptown());
                    bundle.putString(MyAddressEditActivity.KEY_ADDRESS_DOOR, goods.getDoor());

                    intent.putExtras(bundle);
                    ((MyAddressActivity) context).setResult(0, intent);
                    ((MyAddressActivity) context).finish();
                } else
                {

                    SingleChooseDialog singleChooseDialog =
                        new SingleChooseDialog(context, context.getString(R.string.menu_address));
                    singleChooseDialog.addItems(context.getResources().getStringArray(R.array.address_click_menus));
                    singleChooseDialog.setOnItemClickListener(new AdapterView.OnItemClickListener()
                    {

                        @Override
                        public void onItemClick(AdapterView<?> parent, View view, final int singlePosition, long id)
                        {
                            if (singlePosition == 0)
                            {
                                RetrieveDataAsyn.getInstance().updateAddress(goods.getObjectId(), goods.getUserId(),
                                    goods.getName(), goods.getMobile(), goods.getUptown(), goods.getDoor(), 0,
                                    new RetrieveDataListener()
                                {

                                    @Override
                                    public void dataRetrieved(int requestId, Object data)
                                    {
                                        if (data == null)
                                        {
                                            UtilTools.showMyToast(context, context.getString(R.string.failed));
                                        } else
                                        {
                                            UtilTools.showMyToast(context, context.getString(R.string.success));
                                            ShippingAddr ship = list.remove(position);
                                            list.add(0, ship);
                                            notifyDataSetChanged();
                                        }
                                    }
                                });
                            } else if (singlePosition == 1)
                            {
                                Intent intent = new Intent(context, MyAddressEditActivity.class);
                                Bundle bundle = new Bundle();
                                bundle.putInt(MyAddressEditActivity.KEY_MODE, MyAddressEditActivity.MODE_EDIT);
                                bundle.putString(MyAddressEditActivity.KEY_ADDRESS_OBJECTID, goods.getObjectId());
                                bundle.putString(MyAddressEditActivity.KEY_ADDRESS_NAME, goods.getName());
                                bundle.putString(MyAddressEditActivity.KEY_ADDRESS_MOBILE, goods.getMobile());
                                bundle.putString(MyAddressEditActivity.KEY_ADDRESS_UPTOWN, goods.getUptown());
                                bundle.putString(MyAddressEditActivity.KEY_ADDRESS_DOOR, goods.getDoor());

                                intent.putExtras(bundle);
                                context.startActivity(intent);
                            } else if (singlePosition == 2)
                            {
                                // 删除
                                RetrieveDataAsyn.getInstance().deleteAddress(goods.getObjectId(), 0,
                                    new RetrieveDataListener()
                                {

                                    @Override
                                    public void dataRetrieved(int requestId, Object data)
                                    {
                                        if (data == null)
                                        {
                                            UtilTools.showMyToast(context, context.getString(R.string.failed));
                                        } else
                                        {
                                            UtilTools.showMyToast(context, context.getString(R.string.success));
                                            list.remove(position);
                                            notifyDataSetChanged();
                                            if(list.size()==0)
                                            {
                                                ((MyAddressActivity)context).refreshView();  
                                            }
                                                
                                        }
                                    }
                                });
                            }
                        }
                    });
                    singleChooseDialog.show();

                }
            }
        });
        viewHolder.edit.setOnClickListener(new View.OnClickListener()
        {
            
            @Override
            public void onClick(View v)
            {
                Intent intent = new Intent(context, MyAddressEditActivity.class);
                Bundle bundle = new Bundle();
                bundle.putInt(MyAddressEditActivity.KEY_MODE, MyAddressEditActivity.MODE_EDIT);
                bundle.putString(MyAddressEditActivity.KEY_ADDRESS_OBJECTID, goods.getObjectId());
                bundle.putString(MyAddressEditActivity.KEY_ADDRESS_NAME, goods.getName());
                bundle.putString(MyAddressEditActivity.KEY_ADDRESS_MOBILE, goods.getMobile());
                bundle.putString(MyAddressEditActivity.KEY_ADDRESS_UPTOWN, goods.getUptown());
                bundle.putString(MyAddressEditActivity.KEY_ADDRESS_DOOR, goods.getDoor());

                intent.putExtras(bundle);
                context.startActivity(intent); 
            }
        });
        return convertView;
    }

    class ViewHolder
    {

        public TextView name;
        public TextView mobile;
        public TextView uptown;
        public TextView door;
        public View defaultView;
        public Button  edit;

    }

}
